package com.dao.impl;

import com.dao.UserDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.pojo.TbUser;
import com.utils.DBUtils;
import com.utils.SqlUtils;

import java.sql.SQLException;

public class UserDaoImpl implements UserDao {

    private QueryRunner queryRunner=new QueryRunner(DBUtils.getSource());

    @Override
    public void addUser(TbUser user) throws SQLException {
        String sql = SqlUtils.getSql("user.add");
        long result = queryRunner.insert(sql, new ScalarHandler<>(), user.getUsername(), user.getPassword(), user.getNickname(), user.getEmail(), user.getGender(), user.getCode());
        System.err.println(result);

    }

    @Override
    public int active(String username, String code) throws SQLException {
        String sql=SqlUtils.getSql("user.active");
        int result = queryRunner.update(sql, username, code);
        return result;
    }

    @Override
    public TbUser findUserByName(String username) throws SQLException {
        String sql = SqlUtils.getSql("user.find.by.name");
        return queryRunner.query(sql, new BeanHandler<>(TbUser.class), username);

    }
}